--- created: source_filename: /home/runner/work/mknodes/mknodes/mknodes/pages/mkclasspage/__init__.py source_function: MkClassPage.__init__ source_line_no: 46 hide: - toc icon: octicons/table-24 status: new template: SUMMARY.html title: MkHtmlTable --- [:fa-brands-github: Show source on GitHub](https://github.com/phil65/mknodes/blob/main/mknodes/basenodes/mkhtmltable/__init__.py) ### Class representing a html table. !!! info "Description" Compared to MkTable, this will end up with a more verbose output, but it can contain more complex Markdown in cells. === "Examples" ### Example: **Regular** !!! jinja "Jinja" ``` {.jinja } {% set code_col = [_mk.MkCode("print('hello world') sys.exit()"), _mk.MkCode("print('hello world') sys.exit()")] %} {% set admonitions = [_mk.MkAdmonition("Admonition inside cell"), _mk.MkAdmonition("Admonition inside cell")] %} {% set tabs = [_mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"])), _mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"]))] %} {% set data = {"Code": code_col, "Admonitions": admonitions, "Tabs": tabs} %} {{ data | MkHtmlTable }} ``` !!! python "Python" ``` {.python } MkHtmlTable( data={ "Admonitions": [ MkAdmonition("Admonition inside cell"), MkAdmonition("Admonition inside cell"), ], "Code": [ MkCode("print('hello world')\nsys.exit()"), MkCode("print('hello world')\nsys.exit()"), ], "Tabs": [MkTabbed(tabs=[...]), MkTabbed(tabs=[...])], } ) ``` ===! "Rendered"
Code Admonitions Tabs
```` {.python } print('hello world') sys.exit() ```` !!! info Admonition inside cell ===! "A" Tab a === "B" Tab b
```` {.python } print('hello world') sys.exit() ```` !!! info Admonition inside cell ===! "A" Tab a === "B" Tab b
=== "Markdown" ``` {.markdown }
Code Admonitions Tabs
```` {.python } print('hello world') sys.exit() ```` !!! info Admonition inside cell ===! "A" Tab a === "B" Tab b
```` {.python } print('hello world') sys.exit() ```` !!! info Admonition inside cell ===! "A" Tab a === "B" Tab b
``` === "Html" ``` {.html }
Code Admonitions Tabs
print('hello world')
        sys.exit()
        
!!! info Admonition inside cell ===! "A" Tab a === "B" Tab b
print('hello world')
        sys.exit()
        
!!! info Admonition inside cell ===! "A" Tab a === "B" Tab b
``` === "Repr tree" ``` MkHtmlTable ├── MkCode("print('hello world')\nsys.exit()") │ ╰── MkText("print('hello world')\nsys.exit()") ├── MkCode("print('hello world')\nsys.exit()") │ ╰── MkText("print('hello world')\nsys.exit()") ├── MkAdmonition('Admonition inside cell') │ ╰── MkText('Admonition inside cell') ├── MkAdmonition('Admonition inside cell') │ ╰── MkText('Admonition inside cell') ├── MkTabbed(tabs=[...]) │ ├── MkTab(title='A', content='Tab a', new=True) │ │ ╰── MkText('Tab a') │ ╰── MkTab(title='B', content='Tab b') │ ╰── MkText('Tab b') ├── MkTabbed(tabs=[...]) │ ├── MkTab(title='A', content='Tab a', new=True) │ │ ╰── MkText('Tab a') │ ╰── MkTab(title='B', content='Tab b') │ ╰── MkText('Tab b') ``` === "DocStrings" ::: mknodes.MkHtmlTable options: show_docstring_description: False === "Base classes" | Name | Children | Inherits | |--- | --- | --- | | **[MkBaseTable](https://phil65.github.io/mknodes/)**
*mknodes.basenodes.mkbasetable*
Base Class for MkTables\. Only deals with managing the data\. | | | === "⋔ Inheritance diagram" ``` mermaid graph TD 94875592865136["mkhtmltable.MkHtmlTable"] 94875593531088["mkbasetable.MkBaseTable"] 94875590900096["mkcontainer.MkContainer"] 94875594508576["mknode.MkNode"] 94875592833088["node.Node"] 140216716431552["builtins.object"] 94875593531088 --> 94875592865136 94875590900096 --> 94875593531088 94875594508576 --> 94875590900096 94875592833088 --> 94875594508576 140216716431552 --> 94875592833088 ``` === "NodeFile" ``` {.toml title='/home/runner/work/mknodes/mknodes/mknodes/basenodes/mkhtmltable/metadata.toml'} [metadata] icon = "octicon:table-24" status = "new" name = "MkHtmlTable" [examples.regular] title = "Regular" jinja = """ {% set code_col = [_mk.MkCode("print('hello world')\nsys.exit()"), _mk.MkCode("print('hello world')\nsys.exit()")] %} {% set admonitions = [_mk.MkAdmonition("Admonition inside cell"), _mk.MkAdmonition("Admonition inside cell")] %} {% set tabs = [_mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"])), _mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"]))] %} {% set data = {"Code": code_col, "Admonitions": admonitions, "Tabs": tabs} %} {{ data | MkHtmlTable }} """ [output.markdown] template = """ {% set table = node.data %} {% for header in table.keys() %} {% endfor %} {% for row in node.iter_rows() %} {% for item in row %} {% endfor %} {% endfor %}
{{ header }}
{{ item }}
""" ``` === "Code" ``` {.python title='mknodes.basenodes.mkhtmltable.MkHtmlTable' linenums='10'} class MkHtmlTable(mkbasetable.MkBaseTable): """Class representing a html table. Compared to MkTable, this will end up with a more verbose output, but it can contain more complex Markdown in cells. """ STATUS = "new" def get_element(self) -> xml.Table | None: table_data = self.data # property if not any(table_data[k] for k in table_data): return None root = xml.Table(markdown=True) data = [[str(k) for k in row] for row in self.iter_rows()] headers = list(table_data.keys()) data.insert(0, headers) for items in data: tr = xml.Tr(parent=root) for item in items: td = xml.Td(parent=tr) td.text = "\n" + item + "\n" return root def _to_markdown(self) -> str: root = self.get_element() return root.to_string(space="") if root is not None else "" ```